package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.JianzhengrenAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.StudentListTask;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 17/1/3.
 */

public class ChooseJianzhengrenActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener,JianzhengrenAdapter.OnAdapterClickListener{

    private Context mContext;
    private RelativeLayout titleLayout;
    private TextView back;
    private ImageView search;
    private PullToRefreshListView friendList;
    private int mFriendListTaskId = -1;
    private int currentPage = 1;
    private int size = 20;
    private JianzhengrenAdapter mAapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_jianzhengren);
        mContext = this;
        initView();

    }

    private void initView() {
        titleLayout = (RelativeLayout) findViewById(R.id.title_layout);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        search = (ImageView) findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,SearchChiBanActivity.class);
                intent.putExtra("From",1);
                startActivityForResult(intent,121);
            }
        });
        friendList = (PullToRefreshListView) findViewById(R.id.friend_list);
        mAapter = new JianzhengrenAdapter(mContext);
        mAapter.setOnAdapterClickListener(this);
        friendList.init(mContext,true);
        friendList.setOnRefreshListener(this);
        friendList.setOnLoadDataListener(this);
        friendList.setAdapter(mAapter,true);
        friendList.doRefresh();
    }

    private void getFriendList() {
        if(mFriendListTaskId != -1){
            return;
        }
        if(currentPage == 0){
            friendList.setNoNextPage();
            return;
        }
        StudentListTask task = new StudentListTask(size,currentPage,0);
        task.addListener(mListener);
        mFriendListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == mFriendListTaskId){
                mFriendListTaskId = - 1;
                if(currentPage == 1){
                    mAapter.clearData();
                    friendList.onRefreshComplete();
                    friendList.enableLoadMore();
                }else{
                    friendList.onLoadDataComplete(true);
                }
                List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
                if(data != null){
                    fansList = (List<EatShowUserInfo>) data;
                }

                if(fansList != null && fansList.size()>0){
                    mAapter.updateData(fansList);
                }

                currentPage = result;
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            mFriendListTaskId = -1;
            if (currentPage == 1){
                friendList.onRefreshComplete();
            }else {
                friendList.onLoadDataComplete(false);
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 121 && data != null){
            setResult(RESULT_OK,data);
            finish();
        }
    }

    @Override
    public void OnAdapterClick(EatShowUserInfo info) {
        if (info != null) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("user",info);
            intent.putExtras(bundle);
            setResult(RESULT_OK,intent);
            finish();
        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getFriendList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getFriendList();
    }
}
